package org.bookie.web.pages;

import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.bookie.model.Group;
import org.bookie.model.services.GroupDAO;
import org.bookie.web.framework.CommonExternalPage;

/**
 * Page displaying {@link Group} details like name, description, members etc.
 * 
 * @author Igor Drobiazko
 *
 */
public abstract class GroupDetails extends CommonExternalPage 
	implements PageBeginRenderListener, IExternalPage{
	
	/**
	 * {@inheritDoc}
	 */
	public void pageBeginRender(PageEvent event) {
		if(null!=getGroup()){
			setGroup(getGroupDAO().findByName(getGroup().getName()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void activateExternalPage(Object[] parameters, IRequestCycle cycle) {
		String name = null;
		if (0<parameters.length) {
			name = (String) parameters[0];
		}
		if (null == name) {
			recordError("groupdetails.name.missing");
			return;
		}
		Group group = getGroupDAO().findByName(name);
		if(null==group){
			recordError("groupdetails.group.notfound");
		}
		setGroup(group);
	}
	
	/**
	 * Get the {@link Group} to view details of 
	 * @return instance of Group
	 */
	@Persist("client")
	public abstract Group getGroup();
	
	/**
	 * Set the {@link Group} to view details of 
	 * @param group group to set
	 */
	public abstract void setGroup(Group group);
	
	/**
	 * Injected group service
	 * @return instance of GroupDAO
	 */
	@InjectObject("service:bookie.model.GroupDAO")
	public abstract GroupDAO getGroupDAO();

}
